package algo;

import java.util.ArrayList;

import objects.AccessPoint;
import objects.Point;

public class Trilateration {
	
	/**
	 * Returns the position of the mobile by trilateration
	 * @param accessPoints 
	 * @param distances
	 * @param xmin
	 * @param xmax
	 * @param ymin
	 * @param ymax
	 * @param path
	 * @return
	 */
	public static Point getResult(ArrayList<AccessPoint> accessPoints, ArrayList<Float> distances, float xmin, float xmax, float ymin, float ymax, float path) {
		
		float min = Float.MAX_VALUE;
		float xm = xmin;
		float x = xmin;
		float ym = ymin;
		float y = ymin;
		
		while (x <= xmax) {
			
			y = ymin;
			while (y <= ymax) {
				
				float dmax = 0;
				
				for (int i = 0; i < accessPoints.size() ; i++) {
					
					float d = Distance.computeDistance(new Point(x, y, 0), accessPoints.get(i).getCoordinates(), distances.get(i));
					
					if (d > dmax)
						dmax = d;					
				}	
				
				if (dmax < min) {
					min = dmax;
					xm = x;
					ym = y;
				}				
				y += path;				
			}
			x += path;
		}
	return new Point(xm, ym, 0);
	}
}
